package com.shiguo365.market.display.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.presenter.contract.IMarketPricePresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MarketPriceAdapter extends RecyclerView.Adapter<MarketPriceAdapter.ViewHolder> {

    private static final String TAG = "MarketPriceAdapter";

    private final Activity activity;
    private final LayoutInflater inflater;
    private final List<JSONObject> objects;
    private IMarketPricePresenter marketPricePresenter;

    private boolean loading = false;

    public MarketPriceAdapter(@NonNull Activity activity, @NonNull IMarketPricePresenter marketPricePresenter, @NonNull List<JSONObject> objects) {
        this.activity = activity;
        inflater = LayoutInflater.from(activity);
        this.objects = objects;
        this.marketPricePresenter = marketPricePresenter;
    }

    @Override
    public int getItemCount() {
        return objects.size() ;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.activity_market_price_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.update(position);
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.product_item_tv_title)
        protected TextView tvTitle;

        @BindView(R.id.product_item_tv_spec)
        protected TextView tvSpec;

        @BindView(R.id.product_item_tv_from)
        protected TextView tvFrom;

        @BindView(R.id.product_item_tv_price)
        protected TextView tvPrice;

        @BindView(R.id.product_item_btn_cart)
        protected Button btnCart;
//
//        @BindView(R.id.user_order_item_tv_status)
//        protected TextView tvStatus;

        private JSONObject object;

        private boolean isAdded = false;

        protected ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        protected void update(int position) {
            object = objects.get(position);
            Log.d(TAG,object.toJSONString());
            tvTitle.setText(object.getString("name"));
            tvSpec.setText("规格:" + object.getString("quantity"));

            if(object.getString("origin")==""){
                tvFrom.setText("中国");
            }else{
                tvFrom.setText(object.getString("origin"));
            }


            tvPrice.setText("￥" + object.getString("price"));
        }


        @OnClick(R.id.product_item_btn_cart)
        public void onBtnCartOnClick(View view){

            marketPricePresenter.updateCart(MyApplication.getUID(), object.getInteger("id"));
            if(isAdded){
                btnCart.setText("加入购物车");
            }else{
                btnCart.setText("取消加入");
            }
            isAdded = !isAdded;

        }
    }

}
